package com.yanqu.road.dao.impl.activity.jiuzhou.guess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.guess.CrossJiuZhouUserGuessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouUserGuessDataDaoImpl extends TempDataObjectDao<CrossJiuZhouUserGuessData> {
    public static String getCrossJiuZhouUserGuessDataSql = "select * from `t_c_jiu_zhou_user_guess_data` where `activityId`=? and `userId`=?";
    public CrossJiuZhouUserGuessData getCrossJiuZhouUserGuessData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouUserGuessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getCrossJiuZhouUserGuessDataMapSql = "select * from `t_c_jiu_zhou_user_guess_data` where `activityId`=?";
    public Map<Long,CrossJiuZhouUserGuessData> getCrossJiuZhouUserGuessMapData(int activityId) {
        Map<Long,CrossJiuZhouUserGuessData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouUserGuessDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouUserGuessData parse = parse(rs);
                map.put(parse.getUserId(),parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public static String updateCrossJiuZhouUserGuessDataSql = "update `t_c_jiu_zhou_user_guess_data` set guessDataMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossJiuZhouUserGuessData crossJiuZhouUserGuessData) {
        crossJiuZhouUserGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserGuessData.getGuessDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouUserGuessData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouUserGuessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouUserGuessDataSql = "insert into `t_c_jiu_zhou_user_guess_data`(activityId,userId,guessDataMap) values (?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouUserGuessData crossJiuZhouUserGuessData) {
        crossJiuZhouUserGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouUserGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouUserGuessData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouUserGuessData.getGuessDataMap())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouUserGuessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouUserGuessData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouUserGuessData crossJiuZhouUserGuessData = new CrossJiuZhouUserGuessData();
        crossJiuZhouUserGuessData.setActivityId(rs.getInt("activityId"));
        crossJiuZhouUserGuessData.setUserId(rs.getLong("userId"));
        crossJiuZhouUserGuessData.setGuessDataMap(JSON.parseObject(rs.getString("guessDataMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {}));

        return crossJiuZhouUserGuessData;
    }
}
